<?php
/**
 * Created by PhpStorm.
 * User: Sunqj
 * Date: 2017/1/11
 * Time: 下午9:28
 */
namespace app\admin\controller;

use think\Controller;
use think\Image;
use think\Db;

class Utils extends Controller
{
    protected function _initialize()
    {
        if (!session("?H_ADMIN_LOGIN_USER") || session("H_ADMIN_DUE_TIME") < time())
        {
            session("H_ADMIN_REFERER", url("Index/index"));
//            if (strtolower(request()->controller()) === "login")
//            {
//                session("H_ADMIN_REFERER", url("Index/index"));
//            }
//            else
//            {
//                $controller = request()->controller();
//
//                session("H_ADMIN_REFERER", url($controller . "/index"));
//            }

            //判断是否为ajax操作
            if (request()->isAjax())
            {
                die(json_encode(array("status"=>-1,"url"=>url("Login/index"))));
            }
            else
            {
                die("<script>if (window.parent){window.parent.location.href='"
                    . url("Login/index") . "';}else{location.href='"
                    . url("Login/index") . "';}</script>");
            }
        }
    }
    public function home()
    {
        return $this->fetch();
    }

    public function uploader()
    {
        if (request()->isAjax())
        {
            $this->view->engine->layout("public/modalLayout");

            //获取图片元素ID
            $imageId = request()->param("element_id");
            $this->assign("image_id", $imageId);

            $tpl = $this->fetch();

            die(json_encode(array("status"=>0,"html"=>$tpl)));
        }
        else
        {
            return "";
        }
    }
    //清除系统缓存
    public function clearCache()
    {
        //清除缓存
        clear_cache();

        //清除缓存成功
        $this->success('清除缓存成功');
    }

    //获取地区
    public function queryRegion()
    {
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //上级地区ID
            $parentId = request()->param("parent_id");

            //获取地区列表
            $list = Db::name("region")
                ->where("parent_id", $parentId)
                ->where('is_visible',1)
                ->order("order_num desc")
                ->field("id,title")
                ->select();

            return $list;
        }
    }

    public function queryUser(){
        if (request()->isAjax())
        {
            $mobile = request()->param("mobile");
            $id = Db::name("user")
                ->where("mobile", $mobile)
                ->value('id');
            return $id;
        }
    }

    //获取商会
    public function queryBuss()
    {
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //名称或id
            $type = request()->param('type');
            $title = request()->param("title");
            if($type==1){
                $where['title'] = ['like',"%".$title."%"];
            }else{
                $where['id'] = $title;
            }


            //获取地区列表
            $list = Db::name("bussiness")
                ->where($where)
                ->where('is_valid',1)
                ->order("display_order desc,update_time desc")
                ->field("id,title")
                ->select();

            return $list;
        }
    }

    //获取商会下组织部门
    public function queryDepart()
    {
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //上级地区ID
            $bussiness_id = request()->param("bussiness_id");

            //获取地区列表
            $list = Db::name("department")
                ->where("bussiness_id", $bussiness_id)
                ->order("level ASC,display_order desc,update_time desc")
                ->field("id,title,level")
                ->select();
            return $list;
        }
    }

    //获取联盟商
    public function queryUnion()
    {
        //仅支持AJAX操作
        if (request()->isAjax())
        {
            //上级地区ID
            $title = request()->param("title");

            //获取地区列表
            $list = Db::name("union")
                ->where("title",'like', "%".$title."%")
                ->order("display_order desc,update_time desc")
                ->field("id,title")
                ->select();

            return $list;
        }
    }

    //上传单图片
    public function uploadSingleImage()
    {
        //bootstrap-fileinput
        if (request()->isAjax())
        {
            //获取上传图片的名称
            $imageName = request()->param("name");

            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file($imageName);

            //判断文件是否存在
            if (empty($file))
            {
                return array("error"=>"上传图片为空！");
            }

            //保存路径
            $savePath = config("upload_path") . '/' . "images";

            $result = $file
                ->rule("upload_save_name")
                ->validate(array("ext"=>"jpg,png,gif,jpeg"))
                ->move($savePath);

            if ($result)
            {
                $imgPath = "/uploads/images" . '/' . $result->getSaveName();

                return array("status"=>0,"url"=>$imgPath);
            }
            else
            {
                return array("error"=>"图片上传失败！");
            }
        }

        if (request()->isPost())
        {
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file("file");

            //判断文件是否存在
            if (empty($file))
            {
                return array("error"=>"上传图片为空！");
            }

            //保存路径
            $savePath = config("upload_path") . '/' . "images";

            $result = $file
                ->rule("upload_save_name")
                ->validate(array("ext"=>"jpg,png,gif,jpeg"))
                ->move($savePath);

            if ($result)
            {
                $imgPath = "/uploads/images" . '/' . $result->getSaveName();

                die(json_encode(array("name"=>$imgPath)));
            }
            else
            {
                die(json_encode(array("error"=>"图片上传失败！")));
            }
        }
    }

    public function uploadExcel()
    {
        //bootstrap-fileinput
        if (request()->isAjax())
        {
            //获取上传excel的名称
            $imageName = request()->param("name");

            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file($imageName);

            //判断文件是否存在
            if (empty($file))
            {
                return array("error"=>"上传excel文件为空！");
            }

            //保存路径
            $savePath = config("upload_path") . '/' . "excel";

            $result = $file
                ->rule("upload_save_name")
                ->validate(array("ext"=>"xls,xlsx"))
                ->move($savePath);

            if ($result)
            {
                $imgPath = "/uploads/excel" . '/' . $result->getSaveName();

                return array("status"=>0,"url"=>$imgPath);
            }
            else
            {
                return array("error"=>"excel上传失败！");
            }
        }

        if (request()->isPost())
        {
            // 获取表单上传文件 例如上传了001.jpg
            $file = request()->file("file");

            //判断文件是否存在
            if (empty($file))
            {
                return array("error"=>"上传Excel文件为空！");
            }

            //保存路径
            $savePath = config("upload_path") . '/' . "excel";

            $result = $file
                ->rule("upload_save_name")
                ->validate(array("ext"=>"xls,xlsx"))
                ->move($savePath);

            if ($result)
            {
                $imgPath = "/uploads/excel" . '/' . $result->getSaveName();

                die(json_encode(array("name"=>$imgPath)));
            }
            else
            {
                die(json_encode(array("error"=>"excel上传失败！")));
            }
        }
    }

    //删除单张图片
    public function removeSingleImage()
    {
        if (request()->isAjax())
        {

        }
    }

    //验证手机号是否已被使用
    public function isUsedMobile()
    {
        if (request()->isAjax())
        {
            //手机号
            $mobile = request()->param("tel");
            //用户ID
            $userId = request()->param("uid");

            //判断手机号是否已被使用
            $count = Db::name("user")
                ->where("is_valid", 1)
                ->where("id", "NEQ", $userId)
                ->where("tel", $mobile)
                ->count();

            if ($count)
            {
                die("false");
            }
            else
            {
                die("true");
            }
        }
    }

    //验证身份证号
    public function validateIdentity()
    {
        if (request()->isAjax())
        {
            //身份证号
            $identity = request()->param("identity");

            if (empty($identity) || validate_identity($identity))
            {
                die("true");
            }
            else
            {
                die("false");
            }
        }
    }
}